﻿using System;
using System.Runtime.Serialization;
using System.Text;
using System.Web;
using ElectronicCommerce.Framework.Utility;

namespace ElectronicCommerce.Framework.Web.UI
{
    [DataContract]
    public class JsonResult
    {
        public Encoding ContentEncoding
        {
            get;
            set;
        }

        public string ContentType
        {
            get;
            set;
        }

        [DataMember]
        public object Data
        {
            get;
            set;
        }

        public void ExecuteResult(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponse response = context.Response;

            if (!String.IsNullOrEmpty(ContentType))
            {
                response.ContentType = ContentType;
            }
            else
            {
                response.ContentType = "application/json";
            }
            if (ContentEncoding != null)
            {
                response.ContentEncoding = ContentEncoding;
            }
            if (Data != null)
            {
                response.Write(JsonHelper.ObjectToJson(Data));
                response.End();
            }
        }
    }
}
